/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package spwpanel;

import context.Client;
import hibernate.data.Groups;
import hibernate.data.Projects;
import hibernate.data.UserGroups;
import hibernate.data.Users;
import java.util.Date;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author konp
 */
public class Main extends javax.swing.JFrame
{
    //variables
    private static Client client = new Client();
    private Groups selectedGroup = null;

    /**
     * Creates new form Main
     */
    public Main(Client client)
    {
        this.client = client;
        initComponents();
        
        refreshUsers();
        refreshProjects();
        refreshGroups();
        
    }
    
    public void refreshProjects(){
        Session ses = client.getSession();
        
        List projs = ses.createCriteria(Projects.class).list();
        DefaultTableModel model = (DefaultTableModel) projTable.getModel();
        model.setNumRows(projs.size());
        for (int i = 0; i < projs.size(); ++i)
        {
            Projects proj = (Projects) projs.get(i);
           

            model.setValueAt(proj.getPrjName(), i, 0);
            model.setValueAt(proj.getPrjDateCreate(), i, 1);
            model.setValueAt(proj.getPrjKind().getPjkKind(), i, 2);
            model.setValueAt(proj.getPrjManager().getUrsLastName() + " " + proj.getPrjManager().getUrsFirstName(), i, 3);
            if(proj.getPrjDateEnd() != null){
                model.setValueAt("Zakończony", i, 4);
            }else{
                model.setValueAt("W trakcie", i, 4);
            }
                 
            
        }
    }
    
    public void refreshUsers(){
        Session ses = client.getSession();
        
        List users = ses.createCriteria(Users.class).list();
        DefaultTableModel model = (DefaultTableModel) usersTable.getModel();
        model.setNumRows(users.size());
        for (int i = 0; i < users.size(); ++i)
        {
            Users user = (Users) users.get(i);
           

            model.setValueAt(user.getUrsLogin(), i, 0);
            model.setValueAt(user.getUrsLastName(), i, 1);
            model.setValueAt(user.getUrsFirstName(), i, 2);
            model.setValueAt(user.getUrsMail(), i, 3);
            model.setValueAt(user.getUrsKind().getUksName(), i, 4);
        
            
            this.userBox.addItem(user.getUrsLastName() + " " + user.getUrsFirstName());
        }
        
    }
    
    public void refreshGroups(){
        Session ses = client.getSession();
        
        List groups = ses.createCriteria(Groups.class).list();
        
        
        
        DefaultTableModel model = (DefaultTableModel) groupTable.getModel();
        model.setNumRows(groups.size());
        for (int i = 0; i < groups.size(); ++i)
        {
            Groups group = (Groups) groups.get(i);
            int count = ses.createCriteria(UserGroups.class).add(Restrictions.eq("ugrGroup", group)).list().size();

            model.setValueAt(group.getGrpName(), i, 0);
            model.setValueAt(group.getGrpProject().getPrjName(), i, 1);
            model.setValueAt(count, i, 2);
            model.setValueAt(group.getGrpManager().getUrsLastName(), i, 3);
            
            
        }
        
    }
    
    public void refreshUserGroups(){
        
        Session ses = client.getSession();
        List userGroups = this.client.getSession().createCriteria(UserGroups.class).add(Restrictions.eq("ugrGroup", this.selectedGroup)).list();
        
        DefaultTableModel model = (DefaultTableModel) groupUsersTable.getModel();
        model.setNumRows(userGroups.size());
        for (int i = 0; i < userGroups.size(); ++i)
        {
            UserGroups usrGr = (UserGroups) userGroups.get(i);
            

            model.setValueAt(usrGr.getUgrUser().getUrsLogin(), i, 0);
            model.setValueAt(usrGr.getUgrUser().getUrsLastName(), i, 1);
            model.setValueAt(usrGr.getUgrUser().getUrsFirstName(), i, 2);
            model.setValueAt(usrGr.getUgrUser().getUrsKind().getUksName(), i, 3);
            
            
        }
        
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane5 = new javax.swing.JTabbedPane();
        groupsPanel = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        groupTable = new javax.swing.JTable();
        addGroupButton = new javax.swing.JButton();
        editGroupButton = new javax.swing.JButton();
        removeGroupButton = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        groupUsersTable = new javax.swing.JTable();
        addToGroupButton = new javax.swing.JButton();
        removeFromGroupButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        userBox = new javax.swing.JComboBox();
        projectsPanel = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        projTable = new javax.swing.JTable();
        jButton9 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        jButton11 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        usersPanel = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        usersTable = new javax.swing.JTable();
        jPanel6 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        refreshUsers = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("spwPanel");

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Lista grup"));

        groupTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Nazwa", "Projekt", "Liczba członków", "Kierownik"
            }
        ));
        groupTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                groupTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(groupTable);

        addGroupButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/spwpanel/resources/group_add.png"))); // NOI18N
        addGroupButton.setText("Dodaj grupę");
        addGroupButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addGroupButtonActionPerformed(evt);
            }
        });

        editGroupButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/spwpanel/resources/group_edit.png"))); // NOI18N
        editGroupButton.setText("Edytuj grupę");

        removeGroupButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/spwpanel/resources/delete_group.png"))); // NOI18N
        removeGroupButton.setText("Usuń grupę");
        removeGroupButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeGroupButtonActionPerformed(evt);
            }
        });

        jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/spwpanel/resources/refresh.png"))); // NOI18N
        jButton5.setText("Odśwież");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(addGroupButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(editGroupButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(removeGroupButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton5)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addGroupButton)
                    .addComponent(editGroupButton)
                    .addComponent(removeGroupButton)
                    .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(50, 50, 50))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Skład grupy"));

        groupUsersTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Login", "Nazwisko", "Imię", "Typ"
            }
        ));
        jScrollPane3.setViewportView(groupUsersTable);

        addToGroupButton.setText("Dodaj do grupy");
        addToGroupButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addToGroupButtonActionPerformed(evt);
            }
        });

        removeFromGroupButton.setText("Usuń z grupy");
        removeFromGroupButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeFromGroupButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("Dodaj do grupy:");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 405, Short.MAX_VALUE)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(removeFromGroupButton)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(userBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(addToGroupButton)))
                .addContainerGap(172, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addToGroupButton)
                    .addComponent(jLabel1)
                    .addComponent(userBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(removeFromGroupButton)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout groupsPanelLayout = new javax.swing.GroupLayout(groupsPanel);
        groupsPanel.setLayout(groupsPanelLayout);
        groupsPanelLayout.setHorizontalGroup(
            groupsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(groupsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        groupsPanelLayout.setVerticalGroup(
            groupsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(groupsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(groupsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        jTabbedPane5.addTab("Grupy", groupsPanel);

        jPanel7.setBorder(javax.swing.BorderFactory.createTitledBorder("Lista projektów"));

        jScrollPane4.setColumnHeader(null);

        projTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Nazwa", "Data utworzenia", "Rodzaj", "Kierownik projektu", "Status"
            }
        ));
        jScrollPane4.setViewportView(projTable);

        jButton9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/spwpanel/resources/add.png"))); // NOI18N
        jButton9.setText("Dodaj projekt");
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });

        jButton10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/spwpanel/resources/edit.png"))); // NOI18N
        jButton10.setText("Edytuj projekt");
        jButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton10ActionPerformed(evt);
            }
        });

        jButton11.setIcon(new javax.swing.ImageIcon(getClass().getResource("/spwpanel/resources/delete.png"))); // NOI18N
        jButton11.setText("Usuń projekt");
        jButton11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton11ActionPerformed(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/spwpanel/resources/refresh.png"))); // NOI18N
        jButton1.setText("Odśwież");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane4)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addComponent(jButton9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton11)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 53, Short.MAX_VALUE))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton9)
                    .addComponent(jButton10)
                    .addComponent(jButton11)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(48, 48, 48))
        );

        javax.swing.GroupLayout projectsPanelLayout = new javax.swing.GroupLayout(projectsPanel);
        projectsPanel.setLayout(projectsPanelLayout);
        projectsPanelLayout.setHorizontalGroup(
            projectsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(projectsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(419, Short.MAX_VALUE))
        );
        projectsPanelLayout.setVerticalGroup(
            projectsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(projectsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane5.addTab("Projekty", projectsPanel);

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Lista użytkowników"));

        usersTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Login", "Nazwisko", "Imię", "E-mail", "Typ"
            }
        ));
        jScrollPane2.setViewportView(usersTable);

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2)
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(98, Short.MAX_VALUE))
        );

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder("Akcje"));

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/spwpanel/resources/list_add_user.png"))); // NOI18N
        jButton2.setText("Dodaj użytkownika");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/spwpanel/resources/edit_user.png"))); // NOI18N
        jButton3.setText("Edytuj użytkownika");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/spwpanel/resources/list_remove_user.png"))); // NOI18N
        jButton4.setText("Usuń użytkownika");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        refreshUsers.setIcon(new javax.swing.ImageIcon(getClass().getResource("/spwpanel/resources/refresh.png"))); // NOI18N
        refreshUsers.setText("Odśwież");
        refreshUsers.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                refreshUsersActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(refreshUsers, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(refreshUsers, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(0, 0, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30))
        );

        javax.swing.GroupLayout usersPanelLayout = new javax.swing.GroupLayout(usersPanel);
        usersPanel.setLayout(usersPanelLayout);
        usersPanelLayout.setHorizontalGroup(
            usersPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(usersPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(317, Short.MAX_VALUE))
        );
        usersPanelLayout.setVerticalGroup(
            usersPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(usersPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(50, Short.MAX_VALUE))
        );

        jTabbedPane5.addTab("Użytkownicy", usersPanel);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane5)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane5)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        UserWindow userWindow = new UserWindow(client);
        userWindow.setVisible(true);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        String login = (String)usersTable.getValueAt(usersTable.getSelectedRow(), 0);
        
        List users = this.client.getSession().createCriteria(Users.class).add(Restrictions.eq("ursLogin", login)).list();
        
        Users user = (Users) users.get(0);
        
        UserWindow userWindow = new UserWindow(client, user);
        userWindow.setEditWindow(true);
        userWindow.setVisible(true);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void addGroupButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addGroupButtonActionPerformed
        // TODO add your handling code here:        
        GroupsWindow groupsWindow = new GroupsWindow(this.client);
        
        groupsWindow.setVisible(true);
    }//GEN-LAST:event_addGroupButtonActionPerformed

    private void groupTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_groupTableMouseClicked
        // TODO add your handling code here:
        String name = (String)groupTable.getValueAt(groupTable.getSelectedRow(), 0);
        List groups = this.client.getSession().createCriteria(Groups.class).add(Restrictions.eq("grpName", name)).list();
        this.selectedGroup = (Groups) groups.get(0);
        
        refreshUserGroups();
    }//GEN-LAST:event_groupTableMouseClicked

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        String login = (String)usersTable.getValueAt(usersTable.getSelectedRow(), 0);
        List users = this.client.getSession().createCriteria(Users.class).add(Restrictions.eq("ursLogin", login)).list();
        
        Users user = (Users) users.get(0);
        this.client.getSession().beginTransaction();
        this.client.getSession().delete(user);
        this.client.getSession().getTransaction().commit();
        refreshUsers();
    }//GEN-LAST:event_jButton4ActionPerformed

    private void refreshUsersActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refreshUsersActionPerformed
        // TODO add your handling code here:
        refreshUsers();
        
    }//GEN-LAST:event_refreshUsersActionPerformed

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        // TODO add your handling code here:
        ProjectWindow window = new ProjectWindow(this.client);
        window.setVisible(true);
    }//GEN-LAST:event_jButton9ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        refreshProjects();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
        // TODO add your handling code here:
        String name = (String)projTable.getValueAt(projTable.getSelectedRow(), 0);
        List projs = this.client.getSession().createCriteria(Projects.class).add(Restrictions.eq("prjName", name)).list();
        ProjectWindow window = new ProjectWindow(this.client, (Projects)projs.get(0));
        window.setVisible(true);
    }//GEN-LAST:event_jButton10ActionPerformed

    private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton11ActionPerformed
        // TODO add your handling code here:
        String name = (String)projTable.getValueAt(projTable.getSelectedRow(), 0);
        List projs = this.client.getSession().createCriteria(Projects.class).add(Restrictions.eq("prjName", name)).list();
        Projects proj = (Projects) projs.get(0);
        Date newDate = new Date();
        proj.setPrjDateEnd(newDate);
        client.getSession().update(proj);
        refreshProjects();
    }//GEN-LAST:event_jButton11ActionPerformed

    private void addToGroupButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addToGroupButtonActionPerformed
        // TODO add your handling code here:
        String name =(String) this.userBox.getSelectedItem();
        List users = this.client.getSession().createCriteria(Users.class).list();
        Users saveUser = null;
        for(int i = 0; i<users.size(); i++){
            Users user = (Users) users.get(i);
            if ((user.getUrsLastName() + " " + user.getUrsFirstName()).equalsIgnoreCase(name)){
                saveUser = user;
                break;
            }
        }
        Date date = new Date();
        UserGroups usrGr = new UserGroups(date, saveUser, selectedGroup);
        this.client.getSession().beginTransaction();
        this.client.getSession().save(usrGr);
        this.client.getSession().getTransaction().commit();
        refreshUserGroups();
    }//GEN-LAST:event_addToGroupButtonActionPerformed

    private void removeFromGroupButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeFromGroupButtonActionPerformed
        // TODO add your handling code here:
        String login = (String)groupUsersTable.getValueAt(groupUsersTable.getSelectedRow(), 0);
        List users = this.client.getSession().createCriteria(Users.class).add(Restrictions.eq("ursLogin", login)).list();
        
        List usgGr = this.client.getSession().createCriteria(UserGroups.class).add(Restrictions.eq("ugrUser", users.get(0))).list();
        this.client.getSession().beginTransaction();
        client.getSession().delete((UserGroups)usgGr.get(0));
        this.client.getSession().getTransaction().commit();
        refreshUserGroups();
    }//GEN-LAST:event_removeFromGroupButtonActionPerformed

    private void removeGroupButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeGroupButtonActionPerformed
        // TODO add your handling code here:
        String name = (String)groupTable.getValueAt(groupTable.getSelectedRow(), 0);
        List groups = this.client.getSession().createCriteria(Groups.class).add(Restrictions.eq("grpName", name)).list();
        List usgGr = this.client.getSession().createCriteria(UserGroups.class).add(Restrictions.eq("ugrUser", groups.get(0))).list();
        
        this.client.getSession().beginTransaction();
        for (int i = 0; i< usgGr.size(); i++){
            client.getSession().delete((UserGroups)usgGr.get(0));
        }
        client.getSession().delete((Groups)groups.get(0));
        this.client.getSession().getTransaction().commit();
        
        
    }//GEN-LAST:event_removeGroupButtonActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
        refreshGroups();
    }//GEN-LAST:event_jButton5ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try
        {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
            {
                if ("Nimbus".equals(info.getName()))
                {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex)
        {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex)
        {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex)
        {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex)
        {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable()
        {

            public void run()
            { 
                LoginWindow loginWindow = new LoginWindow();
                loginWindow.setClient(client);
                loginWindow.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addGroupButton;
    private javax.swing.JButton addToGroupButton;
    private javax.swing.JButton editGroupButton;
    private javax.swing.JTable groupTable;
    private javax.swing.JTable groupUsersTable;
    private javax.swing.JPanel groupsPanel;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton11;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton9;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTabbedPane jTabbedPane5;
    private javax.swing.JTable projTable;
    private javax.swing.JPanel projectsPanel;
    private javax.swing.JButton refreshUsers;
    private javax.swing.JButton removeFromGroupButton;
    private javax.swing.JButton removeGroupButton;
    private javax.swing.JComboBox userBox;
    private javax.swing.JPanel usersPanel;
    private javax.swing.JTable usersTable;
    // End of variables declaration//GEN-END:variables

//getters and setters
    
     public static void setClient(Client client) {
        Main.client = client;
    }

}
